<?php

    namespace Home\Event;
    use Think\Controller;
    class IndexEvent extends Controller
    {
        //查看所有可以显示的分类
        public function queryAllCate ()
        {
            $cate = M("category");
            $list = $cate->field(array("id","pid", "name", "path", "concat(path,id,',') as dpath")) ->where("is_show=1") ->order("dpath")->select();
            return $list;
        }

        //查询所有的商品
        public function queryAllGoods ()
        {
            $goods = M("goods");
            $goodslist = $goods
                ->alias("g")
                ->join("shopping_goodsimg i on g.id = i.goods_id")
                ->where("i.is_main=1 and g.is_show=1 and g.goods_type=0")
                ->field(array("g.id", "g.name", "g.price", "g.num", "g.sales_num", "g.is_show", "g.is_new", "g.is_hot","g.is_integral", "g.is_coupon", "g.special_id", "g.seckill_id", "g.integral_price","g.category_id", "i.img"))
                ->select();

            return $goodslist;
        }

        //通过商品id查询商品的所有信息  商品详情页使用
        public function queryGoods ()
        {
            $id = I('get.id');
            $goods = M("goods");
            $res = $goods
                ->alias("g")->join("shopping_goodsimg i on g.id=i.goods_id")
                ->field(array("g.id","g.name","g.price","g.num","g.sales_num","g.msg","g.is_integral","g.integral_price","g.is_coupon","i.img"))
                ->where("g.id=$id and i.is_main=1 and i.is_show=1")
                ->find();
            return $res;
        }

        //通过商品id查询商品的所有图片信息  商品详情页使用
        public function queryGoodsImg ()
        {
            $id = I('get.id');
            $goods = M("goodsimg");
            $res = $goods ->where("goods_id=$id and is_show=1")->order("is_main desc")->select();
            return $res;
        }

        //查询所有优惠券
        public function queryAllCoupon()
        {
            $goods_coupon = M("coupon");
            $info = $goods_coupon
                ->where("is_open = 1 and is_lose = 1 and num > 0")//未失效的,已开启的
                ->select();
            return $info;
        }
        //查询商品评价
        public function queryeva(){
            $id = I('get.id');
            $queryeva = M('evaluate')
                ->alias('e')
                ->join('shopping_user as u on u.id=e.user_id')
                ->where("e.goods_id=$id")->select();
            return $queryeva;

        }
        //评价数量
        public function evatot(){
            $id = I('get.id');
            $evatot = M('evaluate')->where("goods_id='$id'")->count();
            return $evatot;
        }
        //商品类型
        public function goods_type(){
            $id = I('get.id');
            $goods = M("goods");
            $goods_type = $goods->field("goods_type")->where("id=$id")->find();
            return $goods_type;
        }
        //抢购商品详情
        public function goods_seckill(){
            $id = I('get.id');
            $goods = M("seckill");
            $goods_seckill = $goods->where("goods_id=$id")->find();
            return $goods_seckill;
        }

        //团购商品详情
        public function goods_group(){
            $id = I('get.id');
            $uid = I('get.uid');
            $goods = M("group");
            $goods_group = $goods->where("goods_id=$id")->find();
            $this->assign('uid',$uid);
//            dump($uid);die;
            return $goods_group;

        }


        //商品分类二级页面
        public function categoryDetails()
        {
            $goods = M("goods");
            $cate = M("category");
            //得到分类id
            $id = I("get.id");
            //通过id查询他是否有子分类
            $children = $cate->getFieldByPid($id,"id");
            //根据id查询path
            $res = $cate->field(array('path','name','pid'))->where("id=$id")->find();
            $dpath = $res['path'].$id.",";
            //该分类名
            $data['name'] = $res['name'];
            if($res['path'] != "0,"){
                //根据他的path查询他所属的商品类别
                $path = $res['path'];
                $temp = substr(rtrim($path,","),2);
                list($topCate) = explode(",",$temp);

                $dpath = "0,".$topCate.",";
                $data['topCateName'] = $cate->getFieldById($topCate,"name");
            }else{
                $data['topCateName'] = $res['name'];
            }

            //查询子分类
            $cateList = $cate->where("path like '$dpath%'")->select();
            $data['cateList'] = $cateList;
            //如果有子分类
            if(!empty($children)){
                foreach($cateList as $key => $value){
                    $category_id = $value['id'];
                    //根据分类id查询所属的商品
                    $res = $goods->alias("g")->join("shopping_goodsimg i on i.goods_id=g.id")
                        ->field(array("g.id","g.name","i.img","g.price"))
                        ->where("g.category_id=$category_id and g.is_show=1 and g.goods_type=0 and i.is_main=1")->select();
                    $goodsdata[$key] = $res;
                }
                $data['goodsList'] = $goodsdata;
            }else{
                //如果没有子分类
//                echo "+++++++++++++++++++++++";
                $category_id = I("get.id");
                //根据分类id查询所属的商品
                $res = $goods->alias("g")->join("shopping_goodsimg i on i.goods_id=g.id")
                    ->field(array("g.id","g.name","i.img","g.price"))
                    ->where("g.category_id=$category_id and g.is_show=1 and g.goods_type=0 and i.is_main=1")->select();
                $data['goodsList'][0] = $res;
            }
            return $data;
        }
    }




